package com.yc.damai.damaiuser.web;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/9/13
 * Email haijunzhou@hnit.edu.cn
 */
@RestController
public class MqDo {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("workMsg")
    public String workMsg(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("workQueue",msg + "  " + i);
        }
        return "工作模式消息";
    }

    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void workeQueue(String msg){
        System.out.println(" 消费方法1: "+msg);
    }

    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void workeQueue2(String msg){
        System.out.println(" 消费方法2: "+msg);
    }


    @RequestMapping("fanoutMsg")
    public String fanoutMsg(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("fanoutEx", "",msg + "  " + i);
        }
        return "广播模式消息";
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanoutQueue1"),
            exchange = @Exchange(name="fanoutEx", type = ExchangeTypes.FANOUT)
    ))
    public void fanoutQueue(String msg){
        System.out.println(" 广播消费方法1: "+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanoutQueue2"),
            exchange = @Exchange(name="fanoutEx", type = ExchangeTypes.FANOUT)
    ))
    public void fanoutQueue2(String msg){
        System.out.println(" 广播消费方法2: "+msg);
    }


    @RequestMapping("topicMsg")
    public String topicMsg(String msg, String key){
        rabbitTemplate.convertAndSend("topicEx", key,msg);
        return "主题模式消息";
    }

    @RabbitListener(bindings = @QueueBinding(
            value =  @Queue,
            exchange = @Exchange(name="topicEx", type=ExchangeTypes.TOPIC),
            key = "user.*"
    ))
    public void topicQueue1(String msg){
        System.out.println(" 主题消费方法1: "+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value =  @Queue,
            exchange = @Exchange(name="topicEx", type=ExchangeTypes.TOPIC),
            key = {"user.insert","order.#"}
    ))
    public void topicQueue2(String msg){
        System.out.println(" 主题消费方法2: "+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value =  @Queue,
            exchange = @Exchange(name="topicEx", type=ExchangeTypes.TOPIC),
            key = {"user.#","order.update"}
    ))
    public void topicQueue3(String msg){
        System.out.println(" 主题消费方法3: "+msg);
    }


}
